package com.yql.controller;

import com.github.pagehelper.PageInfo;
import com.yql.entity.Company;
import com.yql.entity.Yuans;
import com.yql.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CompanyController {
    @Autowired
    CompanyService companyService;
    @RequestMapping("list")
    public String list(Company company, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, Model model){
        PageInfo<Company> info = companyService.listAll(company, pageNum, pageSize);
        model.addAttribute("info",info);
        model.addAttribute("c",company);
        return "list";
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    @ResponseBody
    @RequestMapping("getyid")
    public Yuans getyid(String cname,Model model){
        Yuans yuans = companyService.getyid(cname);
        System.out.println(yuans);
        System.out.println(yuans.getYid());
        return yuans;
    }

    @ResponseBody
    @PostMapping("add")
    public boolean add(Company company){
        Integer add = companyService.add(company);
        if(add>0){
            return true;
        }
        return false;
    }

    @RequestMapping("listyuans")
    public String listYuans(Model model){
        List<Yuans> yuans = companyService.listYuans();
        model.addAttribute("y",yuans);
        return "yuans";
    }

    @RequestMapping("index")
    public String index(@RequestParam(defaultValue = "list") String url,Model model){
        model.addAttribute("url",url);
        return "index";
    }
}
